//
//  TXModel.m
//  AliComSDKDemo
//

#import "PNSBuildModelUtils.h"
#define TX_SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define TX_SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define IS_HORIZONTAL (TX_SCREEN_WIDTH > TX_SCREEN_WIDTH)

#define TX_Alert_NAV_BAR_HEIGHT      55.0
#define TX_Alert_HORIZONTAL_NAV_BAR_HEIGHT      41.0

//竖屏弹窗
#define TX_Alert_Default_Left_Padding         42
#define TX_Alert_Default_Top_Padding          115

/**横屏弹窗*/
#define TX_Alert_Horizontal_Default_Left_Padding      80.0
#define ALIAUTHSDKBUNDLE [[NSBundle bundleForClass:self.class] pathForResource:@"AliAuthResources" ofType:@"bundle"]
@implementation PNSBuildModelUtils

+ (TXCustomModel *)buildFullScreenModel {
   
    TXCustomModel *model = [[TXCustomModel alloc] init];
    model.navIsHidden = YES;
    model.logoIsHidden = NO;
    model.prefersStatusBarHidden = YES;
    model.preferredStatusBarStyle = UIStatusBarStyleLightContent;
    model.sloganIsHidden = YES;
    
    NSString *bundlePath = ALIAUTHSDKBUNDLE;
    //登录
    model.logoImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%s",bundlePath,"rf_logo.png"]];
    
    //电话号码
    model.numberColor = [self colorWithHex:0x666666 alpha: 1];
    model.numberFont = [UIFont boldSystemFontOfSize:28];

    //登录按钮
    model.loginBtnBgImgs = [[NSArray alloc]initWithObjects:
                            [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%s",bundlePath,"icon_login_btn.png"]],
                            [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%s",bundlePath,"icon_login_btn.png"]],
                            [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%s",bundlePath,"icon_login_btn.png"]], nil];
    
    model.loginBtnText = [[NSAttributedString alloc]initWithString:@""];
    
    model.checkBoxImages = [[NSArray alloc]initWithObjects:
                            [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%s",bundlePath,"icon_uncheck@2x.png"]],
                            [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%s",bundlePath,"icon_check@2x.png"]], nil];
    
    model.privacyColors = @[UIColor.lightGrayColor, [self colorWithHex:0x666666 alpha: 1]];
    model.privacyAlignment = NSTextAlignmentCenter;
    model.privacyFont = [UIFont fontWithName:@"PingFangSC-Regular" size:10.0];
    model.privacyPreText = @"勾选表示您已阅读并同意";
    model.privacyOperatorPreText = @"《";
    model.privacyOperatorSufText = @"》";
    model.privacyOne = [[NSArray alloc]initWithObjects:@"用户协议",@"https://www.runfox.com/privacy_policy/policy.html", nil];
    model.privacyTwo = [[NSArray alloc]initWithObjects:@"隐私协议",@"https://www.runfox.com/privacy_policy/privacy.html", nil];
    
    model.checkBoxIsHidden = NO;
    model.checkBoxIsChecked = YES;
    model.checkBoxWH = 17.0;
    
    
    model.changeBtnIsHidden = NO;
    model.changeBtnTitle = [
       [NSAttributedString alloc] initWithString:@"其他方式登录 >"
       attributes:@{NSForegroundColorAttributeName: UIColor.grayColor, NSFontAttributeName : [UIFont boldSystemFontOfSize:14.0]}
    ];
    
    double tempWidth = 2208;
    
    model.logoFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.origin.y = 34;
        frame.size.width = superViewSize.width / tempWidth * 240;
        
        return frame;
    };
    
    model.numberFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.origin.y = 145;
        return  frame;
    };
    
    model.loginBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        double positionX = superViewSize.width/2 - superViewSize.width/4;
        frame = CGRectMake(positionX, superViewSize.height/2 + 10, superViewSize.width/2,  superViewSize.width/2 * 3/19);
    
        return  frame;
    };
    
    model.changeBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        frame.origin.y = 290;
        frame.origin.x = frame.origin.x - 15;
        frame.size = CGSizeMake(150, 30);
        return  frame;
    };
    
    
    return model;
}

+ (TXCustomModel *)buildAlertModel {
    TXCustomModel *model = [[TXCustomModel alloc] init];
    
    model.alertCornerRadiusArray = @[@10, @10, @10, @10];
    model.alertTitleBarColor = [self colorWithHex:0x3971fe alpha: 1];
    model.alertTitle = [[NSAttributedString alloc] initWithString:@"一键绑定" attributes:@{NSForegroundColorAttributeName : UIColor.whiteColor, NSFontAttributeName : [UIFont systemFontOfSize:20.0]}];
    model.alertCloseImage = [UIImage imageNamed:@"icon_close_light"];
    
    model.privacyNavColor = [self colorWithHex:0x3971fe alpha: 1];
    model.privacyNavBackImage = [UIImage imageNamed:@"icon_nav_back_light"];
    model.privacyNavTitleFont = [UIFont systemFontOfSize:20.0];
    model.privacyNavTitleColor = UIColor.whiteColor;
    
    model.logoImage = [UIImage imageNamed:@"rf_logo"];
    model.numberColor = [self colorWithHex:0x3971fe alpha: 1];
    model.numberFont = [UIFont systemFontOfSize:30.0];
    model.loginBtnText = [[NSAttributedString alloc] initWithString:@"一键登录" attributes:@{NSForegroundColorAttributeName : UIColor.whiteColor, NSFontAttributeName : [UIFont systemFontOfSize:20.0]}];

    model.privacyColors = @[UIColor.lightGrayColor, [self colorWithHex:0x3971fe alpha: 1]];
    model.privacyAlignment = NSTextAlignmentCenter;
    model.privacyFont = [UIFont fontWithName:@"PingFangSC-Regular" size:10.0];
    model.privacyOperatorPreText = @"《";
    model.privacyOperatorSufText = @"》";
    //model.checkBoxIsHidden = NO;
    model.checkBoxWH = 17.0;
    model.checkBoxIsHidden = YES;
    model.changeBtnIsHidden = YES;
    model.changeBtnTitle = [[
       NSAttributedString alloc] initWithString:@"切换到其他方式"
       attributes:@{ NSForegroundColorAttributeName: UIColor.whiteColor, NSFontAttributeName : [UIFont systemFontOfSize:18.0] }
    ];
    
    CGFloat ratio = MAX(TX_SCREEN_WIDTH, TX_SCREEN_HEIGHT) / 667.0;
    
    //实现该block，并且返回的frame的x或y大于0，则认为是弹窗谈起授权页
    model.contentViewFrameBlock = ^CGRect(CGSize screenSize, CGSize contentSize, CGRect frame) {
        CGFloat alertX = 0;
        CGFloat alertY = 0;
        CGFloat alertWidth = 0;
        CGFloat alertHeight = 0;
        if ([self isHorizontal:screenSize]) {
            alertX = ratio * TX_Alert_Horizontal_Default_Left_Padding;
            alertWidth = screenSize.width - alertX * 2;
            alertY = (screenSize.height - alertWidth * 0.5) * 0.5;
            alertHeight = screenSize.height - 2 * alertY;
        } else {
            alertX = TX_Alert_Default_Left_Padding * ratio;
            alertWidth = screenSize.width - alertX * 2;
            alertY = TX_Alert_Default_Top_Padding * ratio;
            alertHeight = screenSize.height - alertY * 2;
        }
        return CGRectMake(alertX, alertY, alertWidth, alertHeight);
    };

    model.logoFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        if ([self isHorizontal:screenSize]) {
            return CGRectZero; //横屏时模拟隐藏该控件
        } else {
            frame.origin.y = 10;
            return frame;
        }
    };
    model.sloganFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        if ([self isHorizontal:screenSize]) {
            return CGRectZero; //横屏时模拟隐藏该控件
        } else {
            frame.origin.y = 110;
            return frame;
        }
    };
    model.numberFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        if ([self isHorizontal:screenSize]) {
            frame.origin.y = 20;
            frame.origin.x = (superViewSize.width * 0.5 - frame.size.width) * 0.5 + 18.0;
        } else {
            frame.origin.y = 140;
        }
        return frame;
    };
    model.loginBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        if ([self isHorizontal:screenSize]) {
            frame.origin.y = 60;
            frame.size.width = superViewSize.width * 0.5; //登录按钮最小宽度是其父视图的一半，再小就不生效了
        } else {
            frame.origin.y = 180;
        }
        return frame;
    };
    model.changeBtnFrameBlock = ^CGRect(CGSize screenSize, CGSize superViewSize, CGRect frame) {
        if ([self isHorizontal:screenSize]) {
            return CGRectZero; //横屏时模拟隐藏该控件
        } else {
            return CGRectMake(10, 240, superViewSize.width - 20, 30);
        }
    };
    return model;
}

+ (UIColor *)colorWithHex:(NSInteger)hex alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 green:((float)((hex & 0xFF00) >> 8))/255.0 blue:((float)(hex & 0xFF))/255.0 alpha:alpha];
}

/// 是否是横屏 YES:横屏 NO:竖屏
+ (BOOL)isHorizontal:(CGSize)size {
    return size.width > size.height;
}


@end
